import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class IntersectionOfTwoLinkedLists {

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        Set<ListNode> nodes = new HashSet<>();
        ListNode temp = headA;
        while (temp!=null){
            nodes.add(temp);
            temp = temp.next;
        }
        temp = headB;
        while (temp!=null){
            if (nodes.contains(temp)){
                return temp;
            }
            temp = temp.next;
        }

        return null;

    }
}
